package 力扣链表;

public class Leedcode6Z字形变换 {
    public String convert(String s, int numRows) {
        if(numRows == 1) return s;
        StringBuilder[] sb = new StringBuilder[numRows];
        for (int i = 0; i < numRows; i++) {
            sb[i] = new StringBuilder();
        }
        int index = 0;//下标
        int len = s.length();
        int row = 0;//行
        while(index < len ){
            while (index < len && row < numRows){//如果下标小于len并且当前行小于总行数
                //如果当前要添加的元素小于row就将其添加到sb的当前位row中
                char c = s.charAt(index++);
                sb[row].append(c);
                row++;
            }
            if (index == len) break;

            row = numRows - 2 ;

            while (index < len && row >= 0){
                char c = s.charAt(index++);
                sb[row].append(c);
                row--;
            }

            row += 2;

        }
        StringBuilder res = new StringBuilder();
        for (StringBuilder str:sb) {
            res.append(str);
        }
        return res.toString();
    }
}
